var picker = picker || {};
 

picker.pick = function(E) {
		var worldRay = o3djs.picking.clientPositionToWorldRay(
																E.x,
																E.y,
																viewInfo.drawContext,
																cubeClient.width,
																cubeClient.height);
		
		var selectedCube = picker.cubePick(cubeSelect[cubeSelect.length-1],worldRay);
		
			if(selectedCube){
				for(var num=0;num < cubeSelectNow.length;num++){
					if (selectedCube[0] == cubeSelectNow[num][0]){
						creator.clearWireFrame(num);
						var directionArray = cubeSelectNow[num][3];
						
						for(var n=0;n < 6;n++){
							if(selectedCube[3][n]){
								directionArray[n] = directionArray[n] ? false : true ;
								break;
								}
							}
						if(   !(directionArray[0]||
								directionArray[1]||
								directionArray[2]||
								directionArray[3]||
								directionArray[4]||
								directionArray[5])){
							cubeSelectNow.splice(num,1);
							}
						return selectedCube;						
						
						}
					}
					cubeSelectNow.push(selectedCube);
					cubeSelectNow.sort(sortNumber);
					return selectedCube;
					}
					else if(cubeSelectNow[0]){
						creator.removeAllFrames();
						cubeSelectNow = [];
						}
		}

picker.virtualPick = function(E){
//这个函数仅仅进行点击的检测，不会将点中的cube压入cubeSelectNow数组。
	var worldRay = o3djs.picking.clientPositionToWorldRay(
																E.x,
																E.y,
																viewInfo.drawContext,
																cubeClient.width,
																cubeClient.height);
		
		var selectedCube = picker.cubePick(cubeSelect[cubeSelect.length-1],worldRay);
	return selectedCube;
	}
		

picker.cubePick = function(mass,worldRay){
		var rayIntersectionInfo;
		var selectIntersectionInfo;
		var picked;
		var distance;
		var neardis;
		var selected;
		for(var n=0;n < mass.length;n++){
				rayIntersectionInfo = mass[n].boundingBox.intersectRay(worldRay.near,worldRay.far);
				if (rayIntersectionInfo.intersected){
					
					if (mass[n][0]){
						var selectMass = picker._cubePickDistance(mass[n],worldRay);
						distance = selectMass[0];
						if (distance){
							if (!neardis || neardis > distance){
								neardis = distance;
								selectIntersectionInfo = selectMass[1];
								picked = n;
								selected = selectMass[2];
								}
							}
					    }
						else{
						distance = o3dMath.lengthSquared(o3dMath.subVector(worldRay.near,
																		   rayIntersectionInfo.position));
						if (!neardis || neardis > distance){
							neardis = distance;
							selectIntersectionInfo = rayIntersectionInfo;
							picked = n;
							selected = mass[picked];
							}
						}
					}
			}
		if (!selected){return;}
			
  		var returnDirection = new Array;
		var L = selected.l/2;
		returnDirection[0] = Math.abs(selectIntersectionInfo.position[0] - (selected.x + L)) < 0.0001;
		returnDirection[1] = Math.abs(selectIntersectionInfo.position[0] - (selected.x - L)) < 0.0001;
		returnDirection[2] = Math.abs(selectIntersectionInfo.position[1] - (selected.y + L)) < 0.0001;
		returnDirection[3] = Math.abs(selectIntersectionInfo.position[1] - (selected.y - L)) < 0.0001;
		returnDirection[4] = Math.abs(selectIntersectionInfo.position[2] - (selected.z + L)) < 0.0001;
		returnDirection[5] = Math.abs(selectIntersectionInfo.position[2] - (selected.z - L)) < 0.0001;
		
		var returnArray = [picked,selectIntersectionInfo,mass[picked],returnDirection,selected];
		
		return(returnArray);
	}
	
picker._cubePickDistance = function(mass,worldRay){
	var rayIntersectionInfo;
	var selectIntersectionInfo;
	var distance;
	var neardis;
	var selected;
	for(var n=0;n < mass.length;n++){
			rayIntersectionInfo = mass[n].boundingBox.intersectRay(worldRay.near,worldRay.far);
			if (rayIntersectionInfo.intersected){
				
				if (mass[n][0]){
					var selectMass = picker._cubePickDistance(mass[n],worldRay);
					distance = selectMass[0];
					if (distance){
						if (!neardis || neardis > distance){
							neardis = distance;
							selectIntersectionInfo = selectMass[1];
							selected = selectMass[2];
							}
						}
					}
					else{
					distance = o3dMath.lengthSquared(o3dMath.subVector(worldRay.near,rayIntersectionInfo.position));
					if (!neardis || neardis > distance){
						selectIntersectionInfo = rayIntersectionInfo;
						neardis = distance;
						selected = mass[n];
						}
					}
				}
		}
	var returnArray = [neardis,selectIntersectionInfo,selected];
	return(returnArray);
	}
	

picker.touchPickedFace = function(E){
	if(cubeSelectNow[0]){
		var worldRay = o3djs.picking.clientPositionToWorldRay(
															E.x,
															E.y,
															viewInfo.drawContext,
															cubeClient.width,
															cubeClient.height);
		var selectedMass = new Array;
		var selectedCube;
	
		for (var n=0;n < cubeSelectNow.length;n++){
			selectedMass[n] = cubeSelectNow[n][2];
			}
			
			selectedCube = picker.cubePick(selectedMass,worldRay);
			
			if(selectedCube){
				for(var n=0;n < 6;n++){
					if(selectedCube[3][n]){
						if(cubeSelectNow[selectedCube[0]][3][n]){
							selectedCube[5] = true;
							}
							else{
								selectedCube[5] = false;
								}
						}
					}
				return selectedCube;
				}
		}
	}

	
picker.initDrawPlan = function(pickInfo,incluedeSelected){
	//这个初始化方块平面函数改为画笔模式下专用的。这个函数会创建并返回一个数组，数组里面包含了可以进行绘画的方块（这些方块都是在一个平面上，并且无论这些方块有没有被选择，都会被归入这个数组。）
	if(pickInfo && !pickInfo[2][0]){
		var planCubeList = new Array;
		planCubeList[0] = new Array;
		planCubeList[1] = new Array;
		planCubeList[2] = pickInfo[3];
		planCubeList[3] = new Array;
		var i = pickInfo[2].x;
		var j = pickInfo[2].y;
		var k = pickInfo[2].z;
		var massParent = pickInfo[2].parent;
		
		planCubeList[0].push(pickInfo[2]);
		planCubeList[1].push(pickInfo[0]);
		planCubeList[3].push(true);//第3个参数表示这个cube已经被选中
		
		if(pickInfo[3][0] || pickInfo[3][1]){
			for (var n = 0;n < massParent.length;n++){
				if (!massParent[n][0] && massParent[n].x==i && n!=pickInfo[0]){
					planCubeList[0].push(massParent[n]);
					planCubeList[1].push(n);
					}
				}
			}
			else if(pickInfo[3][2] || pickInfo[3][3]){
			for (var n = 0;n < massParent.length;n++){
				if (!massParent[n][0] && massParent[n].y == j && n!=pickInfo[0]){
						planCubeList[0].push(massParent[n]);
						planCubeList[1].push(n);
					}
				}
				}
				else{
				for (var n = 0;n < massParent.length;n++){
					if (!massParent[n][0] && massParent[n].z == k && n!=pickInfo[0]){
						planCubeList[0].push(massParent[n]);
						planCubeList[1].push(n);
						}
					}
					}
		return planCubeList;
		}
	}

picker.XYToWorldRay = function(x,y){
	return  o3djs.picking.clientPositionToWorldRay(
													x,
													y,
													viewInfo.drawContext,
													cubeClient.width,
													cubeClient.height);
	}
	
picker.intersectRay = function(bounding,near,far){
	return bounding.intersectRay(near,far);
	}
	
	
picker.drawSelectCube = function(mass,E){
	var worldRay = o3djs.picking.clientPositionToWorldRay(
															E.x,
															E.y,
															viewInfo.drawContext,
															cubeClient.width,
															cubeClient.height);
	var rayIntersectionInfo;
	var drawSelectArray = new Array;
	var distance;
	var neardis;
	var picked;
	for (var n = 0;n < mass[0].length;n++){
		//rayIntersectionInfo = mass[0][n].boundingBox.intersectRay(worldRay.near,worldRay.far);
		rayIntersectionInfo = picker.intersectRay(mass[0][n].boundingBox,worldRay.near,worldRay.far);
		if(rayIntersectionInfo.intersected){
			
				distance = o3dMath.lengthSquared(o3dMath.subVector(worldRay.near,rayIntersectionInfo.position));
						if (!neardis || neardis > distance){
							neardis = distance;
							picked = n;
							}
			}
		}
	var addNew = true;
	if(picked && !mass[3][picked]){
		mass[3][picked] = true;
		
		for(var num=0;num < cubeSelectNow.length;num++){
			if (mass[1][picked] == cubeSelectNow[num][0]){
				addNew = false;
				creator.clearWireFrame(num);
				for(var n=0;n < 6;n++){
					if(mass[2][n]){
						//已被选中的面若再次被划过时，会反选。用下面这句。
						//cubeSelectNow[num][3][n] = cubeSelectNow[num][3][n] ? false : true;
						//不会反选，用下面这句
						cubeSelectNow[num][3][n] = cubeSelectNow[num][3][n] || mass[2][n];
						break;
						}
					}
				var directionArray = cubeSelectNow[num][3];
				if(   !(directionArray[0]||
						directionArray[1]||
						directionArray[2]||
						directionArray[3]||
						directionArray[4]||
						directionArray[5])){
					cubeSelectNow.splice(num,1);
					}
				break;
				}
			}
			if(addNew){
				drawSelectArray = [ mass[1][picked],
									rayIntersectionInfo,
									mass[0][picked],
									[mass[2][0],mass[2][1],mass[2][2],mass[2][3],mass[2][4],mass[2][5]],
									mass[0][picked]
									];
				cubeSelectNow.push(drawSelectArray);
				cubeSelectNow.sort(sortNumber);
			}
		}
	}
	

picker.drawColorToCube = function(mass,E){
	var worldRay = o3djs.picking.clientPositionToWorldRay(
															E.x,
															E.y,
															viewInfo.drawContext,
															cubeClient.width,
															cubeClient.height);
	
	var rayIntersectionInfo;
	var drawSelectArray = new Array;
	var distance;
	var neardis;
	var picked;
	
	for (var n = 0;n < mass[0].length;n++){
		rayIntersectionInfo = mass[0][n].boundingBox.intersectRay(worldRay.near,worldRay.far);
		if(rayIntersectionInfo.intersected){
			
				distance = o3dMath.lengthSquared(o3dMath.subVector(worldRay.near,rayIntersectionInfo.position));
						if (!neardis || neardis > distance){
							neardis = distance;
							picked = n;
							}
			}
		
		}
		if(picked && !mass[3][picked]){
				mass[0][picked].red = defultValue.red;
				mass[0][picked].green = defultValue.green;
				mass[0][picked].blue = defultValue.blue;
				mass[0][picked].alpha = defultValue.alpha;
				mass[0][picked].changeColor(defultValue.red,
											   defultValue.green,
											   defultValue.blue,
											   defultValue.alpha);;
				mass[3][picked] = true;
		}
	
	}

picker.getColorByPick = function(pickInfo){
	if(pickInfo){
		defultValue.red = pickInfo[4].red;
		defultValue.green = pickInfo[4].green;
		defultValue.blue = pickInfo[4].blue;
		defultValue.alpha = pickInfo[4].alpha;
		//下面可以写一些更新函数
		
		
		}
	}
	
	
